#include "mysercertestsetting.h"
#include "ui_mysercertestsetting.h"

mySercerTestSetting::mySercerTestSetting(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::mySercerTestSetting)
{
    ui->setupUi(this);

    this->setFixedSize(this->size());//锁定窗口大小

    m_tcpProcess = new mytcpprogress;
    m_thr = new QThread;
    m_thr->start();
    m_tcpProcess->moveToThread(m_thr);

    connect(this,SIGNAL(testConnect(QString,int,int)),m_tcpProcess,SLOT(onNewTcpServiceProgress(QString,int,int)));
    connect(m_tcpProcess,SIGNAL(connected(QString,int)),this,SLOT(onConnect(QString,int)));
    connect(m_tcpProcess,SIGNAL(disconnected(QString,int)),this,SLOT(onDisconnect(QString,int)));
    connect(this,SIGNAL(gotoDisconnect()),m_tcpProcess,SLOT(onGotoDisconnect()));
}

mySercerTestSetting::~mySercerTestSetting()
{
    delete ui;
    m_thr->quit();
    m_thr->wait();
    m_tcpProcess->deleteLater();
    m_thr->deleteLater();

}



QString mySercerTestSetting::getServerAddress()
{
    return ui->editHost->text().simplified();
}

int mySercerTestSetting::getServerPort()
{
    return ui->editPort->text().toInt();
}

void mySercerTestSetting::on_pushButton_clicked()
{
    QString IP = ui->editHost->text().simplified();
    int port = ui->editPort->text().toInt();
    QRegExp re("^(?:(?:\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])\\.){3}(?:\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])$");
    if(!re.exactMatch(IP) || ui->editPort->text().isEmpty() || port < 0 || port > 65535){//检测地址端口号是否规范
        QMessageBox::warning(0,tr("警告"),tr("服务器配置出错"));
        return;
    }

    ui->textEdit->setText(tr("正在尝试连接服务器..."));

    emit testConnect(IP,port,0);
    ui->pushButton->setEnabled(false);


}

void mySercerTestSetting::onConnect(QString,int)
{
    ui->textEdit->setText(tr("服务器连接成功!"));
    this->accept();
}

void mySercerTestSetting::onDisconnect(QString,int)
{
    QMessageBox::warning(0,tr("警告"),tr("服务器连接失败"));
    ui->textEdit->setText(tr("正在配置服务器参数..."));
    ui->pushButton->setEnabled(true);
}


void mySercerTestSetting::on_pushButton_2_clicked()
{
    this->reject();
}

void mySercerTestSetting::myDeleteThis()
{
    emit this->gotoDisconnect();//发送控制子线程主动断开连接的信号

}



